import fetch from 'common/src/utils/fetch'

function down(url, params, method) {
	const remove_params = ['pageNum_', 'pageSize_']
	let queryParam = ''
	for (const key in params) {
	  // 去掉分页
	  if (remove_params.includes(key)) {
	    continue
	  }
	  // 去掉空参数
	  if (params[key] == '') {
	    continue
	  }
	  queryParam += `${key}=${params[key]}&`
	}
	console.log(4444444, `${url}?${queryParam}`)
    fetch({
        url: `${url}?${queryParam}`,
        method: method ? method : 'GET'
    }).then(res => {
        const blob = new Blob([res.data]);
		console.log(555, res.headers)
        let fileName = res.headers['content-disposition'].replace("attachment;filename=", "");
        fileName = decodeURIComponent(fileName);
		if ('download' in document.createElement('a')) { // 非IE下载
            const elink = document.createElement('a');
            elink.download = fileName;
            elink.style.display = 'none';
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href);  // 释放URL 对象
            document.body.removeChild(elink)
        } else { // IE10+下载
            snavigator.msSaveBlob(blob, fileName)
        }
    })
}

export default {
    down
}
